library modal_progress_hud;

import 'package:flutter/material.dart';

class ModalProgressHUD extends StatelessWidget {
  final bool inAsyncCall;
  final double opacity;
  final Color color;
  final Widget progressIndicator;
  final Offset offset;
  final bool dismissible;
  final Widget child;
  final String loadingContent;
  final TextStyle textStyle;

  ModalProgressHUD({
    Key key,
    @required this.inAsyncCall,
    this.opacity = 0.3,
    this.color = Colors.grey,
    this.progressIndicator = const CircularProgressIndicator(),
    this.offset,
    this.dismissible = false,
    this.loadingContent = "",
    this.textStyle,
    @required this.child,
  })  : assert(child != null),
        assert(inAsyncCall != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    if (!inAsyncCall) return child;

    Widget layOutProgressIndicator;
    if (offset == null)
      layOutProgressIndicator = Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          progressIndicator,
          Padding(
            padding: EdgeInsets.only(top: 20),
            child: Text(
              loadingContent,
              style: textStyle == null
                  ? TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      decoration: TextDecoration.none,
                    )
                  : textStyle,
              textAlign: TextAlign.center,
            ),
          )
        ],
      ));
    else {
      layOutProgressIndicator = Positioned(
        child: progressIndicator,
        left: offset.dx,
        top: offset.dy,
      );
    }

    return new Stack(
      children: [
        child,
        new Opacity(
          child:
              new ModalBarrier(dismissible: dismissible, color: Colors.black),
          opacity: opacity,
        ),
        layOutProgressIndicator,
      ],
    );
  }
}
